package servlet;

import com.alibaba.fastjson2.JSON;
import entity.User;
import service.UserService;
import service.impl.UserServiceImp;
import utils.Page;
import utils.RequestUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/api/UserServlet")
public class UserServlet extends HttpServlet {
    UserService userService = new UserServiceImp();

    /**
     * 查询servlet-不带参数
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String name = req.getParameter("name");
        Integer pageNo = RequestUtil.parseInt(req, "pageNo", 1);
        Integer pageSize = RequestUtil.parseInt(req, "pageSize", 3);

        Page page = new Page();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        User user = new User();
        user.setUsername(name);

        //获取列表数据
        List<User> list = userService.getPage(page,user);
        page.setData(list);

        //获取总数据量
        Integer count = userService.count(user);
        //在设置总数据量的同时能够计算出总页数
        page.setTotalCount(count);

        PrintWriter writer = resp.getWriter();
        writer.println(JSON.toJSONString(page));
        writer.flush();
        writer.close();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //接收前端传递过来的id
        String id = req.getParameter("id");
        Integer count = userService.deleteById(Integer.parseInt(id));
        PrintWriter writer = resp.getWriter();
        writer.println(JSON.toJSONString(count));
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");

        //接收前端值
        String username = req.getParameter("username");
        //String is_deleted = req.getParameter("is_deleted");
        String gender = req.getParameter("gender");
        String phone = req.getParameter("phone");
        Date birthday = RequestUtil.parseDate(req, "birthday", null, "yyyy-MM-dd");

        User user = new User();
        user.setBirthday(birthday);
        user.setUsername(username);
        user.setGender(Integer.parseInt(gender));
        user.setIs_deleted(RequestUtil.parseInt(req, "is_deleted", null));
        user.setPhone(phone);

        Integer count = userService.add(user);

        Map map = new HashMap();
        PrintWriter writer = resp.getWriter();
        if (count > 0) {
            map.put("code", 1);
            map.put("msg", "成功");
        } else {
            map.put("code", 0);
            map.put("msg", "失败");
        }
        writer.println(JSON.toJSONString(map));
        writer.flush();
        writer.close();
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }


}
